package exerciseFrames;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import avatarFrame.AvatarInfoPanel;

import person.PersonRecorder;
import constants.Constants;
import controler.FileControler;
import engine.AvartaEngine;
import exerciseRecord.ExerciseRecorder;
import frames.MainFrame;

public class ExercisePanel extends JPanel {
	private MainFrame mainFrame;
	private EventHandler eventHandler;

	private ExerciseRecordFrame exerciseRecordFrame;

	private JButton runningButton, armButton, legButton, absButton, homeButton, saveButton, openButton, caloriInputButton;
	private JLabel logo, dateLabel_front, caloriLabel_front, caloriLabel_back;
	private JTextField dateField, caloriField;

	private JPanel mainPanel;
	private RunningPanel runningPanel;
	private ArmPanel armPanel;
	private LegPanel legPanel;
	private AbsPanel absPanel;

	private PersonRecorder personRecorder;
	private int indexOfSavedPersonRecorder;

	public ExercisePanel() {
		eventHandler = new EventHandler();

		this.setLayout(null);
		this.setSize(Constants.PANEL_SIZE_WIDTH, Constants.PANEL_SIZE_HEIGHT);
		this.setBackground(Constants.PANEL_BACKGROUND_COLOR);

		// main panel
		mainPanel = new JPanel();
		mainPanel.setLayout(null);
		mainPanel.setSize(Constants.PANEL_SIZE_WIDTH, Constants.PANEL_SIZE_HEIGHT);
		mainPanel.setBackground(Constants.PANEL_BACKGROUND_COLOR);
		this.add(mainPanel);

		// logo
		logo = new JLabel(new ImageIcon(Constants.EXERCISE_PANEL_IMG));
		logo.setBounds(0, 0, 300, 27);
		mainPanel.add(logo);

		// running button
		runningButton = new JButton(new ImageIcon(Constants.RUNNING_IMG));
		runningButton.setBounds(30, 60, 50, 50);
		runningButton.setActionCommand("running");
		runningButton.addActionListener(eventHandler);
		mainPanel.add(runningButton);

		// arm button
		armButton = new JButton(new ImageIcon(Constants.ARM_IMG));
		armButton.setBounds(92, 60, 50, 50);
		armButton.setActionCommand("arm");
		armButton.addActionListener(eventHandler);
		mainPanel.add(armButton);

		// leg button
		legButton = new JButton(new ImageIcon(Constants.LEG_IMG));
		legButton.setBounds(163, 60, 50, 50);
		legButton.setActionCommand("leg");
		legButton.addActionListener(eventHandler);
		mainPanel.add(legButton);

		// abs button
		absButton = new JButton(new ImageIcon(Constants.ABS_IMG));
		absButton.setBounds(225, 60, 50, 50);
		absButton.setActionCommand("abs");
		absButton.addActionListener(eventHandler);
		mainPanel.add(absButton);

		// date components
		dateLabel_front = new JLabel("Date :");
		dateLabel_front.setBounds(52, 190, 40, 25);
		mainPanel.add(dateLabel_front);

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd   HH시mm분");
		dateField = new JTextField(simpleDateFormat.format(Calendar.getInstance().getTime()));
		dateField.setBounds(92, 190, 150, 25);
		mainPanel.add(dateField);

		// calori components
		caloriLabel_front = new JLabel("Calori Intake:");
		caloriLabel_front.setBounds(12, 132, 80, 25);
		mainPanel.add(caloriLabel_front);

		caloriField = new JTextField();
		caloriField.setBounds(92, 132, 100, 25);
		mainPanel.add(caloriField);

		caloriLabel_back = new JLabel("kcal");
		caloriLabel_back.setBounds(193, 132, 30, 25);
		mainPanel.add(caloriLabel_back);
		
		caloriInputButton = new JButton(new ImageIcon(Constants.APPLY_SMALL_IMG));
		caloriInputButton.setBounds(221, 132, 30, 30);
		caloriInputButton.setActionCommand("caloriInput");
		caloriInputButton.addActionListener(eventHandler);
		mainPanel.add(caloriInputButton);

		// home button
		homeButton = new JButton(new ImageIcon(Constants.HOME_IMG));
		homeButton.setBounds(30, 250, 30, 30);
		homeButton.setActionCommand("home");
		homeButton.addActionListener(eventHandler);
		mainPanel.add(homeButton);

		// save button
		saveButton = new JButton(new ImageIcon(Constants.SAVE_IMG));
		saveButton.setBounds(245, 250, 30, 30);
		saveButton.setActionCommand("save");
		saveButton.addActionListener(eventHandler);
		mainPanel.add(saveButton);

		// open button
		openButton = new JButton(new ImageIcon(Constants.OPEN_SMALL_IMG));
		openButton.setBounds(183, 250, 30, 30);
		openButton.setActionCommand("open");
		openButton.addActionListener(eventHandler);
		mainPanel.add(openButton);

		// running panel
		runningPanel = new RunningPanel();
		runningPanel.setLocation(0, 0);
		this.add(runningPanel);
		runningPanel.setVisible(false);

		// arm panel
		armPanel = new ArmPanel();
		armPanel.setLocation(0, 0);
		this.add(armPanel);
		armPanel.setVisible(false);

		// leg panel
		legPanel = new LegPanel();
		legPanel.setLocation(0, 0);
		this.add(legPanel);
		legPanel.setVisible(false);

		// abs panel
		absPanel = new AbsPanel();
		absPanel.setLocation(0, 0);
		this.add(absPanel);
		absPanel.setVisible(false);

		// exercise record frame
		exerciseRecordFrame = new ExerciseRecordFrame();
		exerciseRecordFrame.setVisible(false);

	}

	public MainFrame getMainFrame() {
		return this.mainFrame;
	}

	public void init(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
		runningPanel.init(this);
		armPanel.init(this);
		legPanel.init(this);
		absPanel.init(this);
	}

	public void setPersonRecorder(PersonRecorder personRecorder, int indexOfSavedPersonRecorder) {
		this.personRecorder = personRecorder;

		ExerciseRecorder exerciseRecorder = new ExerciseRecorder();
		personRecorder.getExerciseRecorderArray().add(exerciseRecorder);

		this.indexOfSavedPersonRecorder = indexOfSavedPersonRecorder;

	}

	public PersonRecorder getPersonRecorder() {
		return personRecorder;
	}

	public void setExercisePanel() {
		mainPanel.setVisible(true);
		runningPanel.setVisible(false);
		armPanel.setVisible(false);
		legPanel.setVisible(false);
		absPanel.setVisible(false);
	}

	public void setRunningPanel() {
		mainPanel.setVisible(false);
		runningPanel.setVisible(true);
		armPanel.setVisible(false);
		legPanel.setVisible(false);
		absPanel.setVisible(false);
	}

	public void setArmPanel() {
		mainPanel.setVisible(false);
		runningPanel.setVisible(false);
		armPanel.setVisible(true);
		legPanel.setVisible(false);
		absPanel.setVisible(false);
	}

	public void setLegPanel() {
		mainPanel.setVisible(false);
		runningPanel.setVisible(false);
		armPanel.setVisible(false);
		legPanel.setVisible(true);
		absPanel.setVisible(false);
	}

	public void setAbsPanel() {
		mainPanel.setVisible(false);
		runningPanel.setVisible(false);
		armPanel.setVisible(false);
		legPanel.setVisible(false);
		absPanel.setVisible(true);
	}

	public void save() {
		// get latest recorder
		ArrayList<ExerciseRecorder> exerciseRecorderArray = personRecorder.getExerciseRecorderArray();
		ExerciseRecorder latestRecorder = exerciseRecorderArray.get(exerciseRecorderArray.size() - 1);

		latestRecorder.setDate(dateField.getText());

		// save exercise info
		FileControler.getFileControler().saveExerciseInfo(personRecorder, indexOfSavedPersonRecorder);

		// open new frame(exercise recorder)
		exerciseRecordFrame.writeExerciseRecord(personRecorder);
		
		// add new ExerciseRecorder
		ExerciseRecorder exerciseRecorder = new ExerciseRecorder();
		personRecorder.getExerciseRecorderArray().add(exerciseRecorder);
		
		// clear previous exercise info
		clear();
	}

	public void clear() {
		AvatarInfoPanel avatarInfoPanel = mainFrame.getAvartaFrame().getAvatarInfoPanel();
		
		avatarInfoPanel.clear();
		absPanel.clear();
		armPanel.clear();
		legPanel.clear();
		runningPanel.clear();
	}
	
	public void inputCalori() {
		AvatarInfoPanel avatarInfoPanel = mainFrame.getAvartaFrame().getAvatarInfoPanel();
		
		int caloriIntake = Integer.parseInt(caloriField.getText());
		avatarInfoPanel.addIntakenCalori(caloriIntake);
	}

	private class EventHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand() == "running") {
				setRunningPanel();
			} else if (e.getActionCommand() == "arm") {
				setArmPanel();
			} else if (e.getActionCommand() == "leg") {
				setLegPanel();
			} else if (e.getActionCommand() == "abs") {
				setAbsPanel();
			} else if (e.getActionCommand() == "home") {
				mainFrame.setMainPanel();
			} else if (e.getActionCommand() == "save") {
				save();
			} else if (e.getActionCommand() == "open") {
				exerciseRecordFrame.writeExerciseRecord(personRecorder);
			} else if( e.getActionCommand() == "caloriInput") {
				inputCalori();
			}

		}

	}

}
